<script setup>
import { ref, getCurrentInstance } from 'vue'
import { addApi, updateApi } from '@/api/user'
import { ElMessage } from 'element-plus'
const instance = getCurrentInstance()
const ruleFormRef = ref(null)
const addShow = () => {
  dialogVisible.value = true
}
const confirm = async () => {
  let res = null;
  if (!ruleFormRef) return
  await ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      if (dialogFormData.value.id) {
        res = await updateApi(dialogFormData.value)
      } else {
        res = await addApi(dialogFormData.value)
      }
      if (res.code) {
        ElMessage.success('操作成功!')
        resetForm()
        instance.emit('search')
      } else {
        ElMessage.error('操作失败！')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

const dialogFormData = ref({
  id: null,
  empNo: null,
  empName: null,
  role: null,
  gender: null,
  phone: null,
  isEffective: '1'
})
const resetForm = () => {
  dialogVisible.value = false
  dialogFormData.value = {
    id: null,
    empName: null,
    role: null,
    gender: null,
    phone: null,
    isEffective: null
  }
  ruleFormRef.value.resetFields()
}
const dialogVisible = ref(false)
const rules = ref({
  id: [
    { required: true, message: '请输入', trigger: 'blur' },
    { min: 3, max: 10, message: '长度应该在3-10', trigger: 'blur' },
  ],
  empName: [
    { required: true, message: '请输入，不能为空', trigger: 'blur' },
  ],
  role: [
    { required: true, message: '请输入，不能为空', trigger: 'blur' },
  ],
  gender: [
    { required: true, message: '请输入，不能为空', trigger: 'blur' },
  ],
  phone: [
    { required: true, message: '请输入，不能为空', trigger: 'blur' },
  ]
})

defineExpose({
  addShow,
  dialogVisible,
  dialogFormData
})
</script>

<template>
  <el-dialog v-model="dialogVisible" style="width: 400px;">
    <el-form ref="ruleFormRef" :model="dialogFormData" :rules="rules">
      <el-row>
        <el-col span="24">
          <el-form-item label="员工编号" prop="empNo">
            <el-input v-model="dialogFormData.empNo" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item label="员工名称" prop="empName">
            <el-input v-model="dialogFormData.empName" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item label="角色" prop="role">
            <el-input v-model="dialogFormData.role" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item label="性别" prop="gender">
            <el-input v-model="dialogFormData.gender" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item label="联系方式" prop="phone">
            <el-input v-model="dialogFormData.phone" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-switch v-model="dialogFormData.isEffective" active-value='1' inactive-value='0' />
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="confirm">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>